﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EuroManager.Domain.Simulator.Engine;
using Rhino.Mocks;

namespace EuroManager.Domain.Simulator.Tests
{
    public static class TestHelper
    {
        private static readonly float timeStep = 0.05f;

        public static IEnumerable<T> Enumerable<T>(params T[] items)
        {
            return items.AsEnumerable();
        }

        public static void Advance(float time, params ITimeAware[] timeAwares)
        {
            Advance(null, time, null, timeAwares);
        }

        public static void Advance(float time, Action action, params ITimeAware[] timeAwares)
        {
            Advance(null, time, action, timeAwares);
        }

        public static void AdvanceWhile(Func<bool> condition, float time, params ITimeAware[] timeAwares)
        {
            Advance(condition, time, null, timeAwares);
        }

        private static void Advance(Func<bool> condition, float time, Action action, params ITimeAware[] timeAwares)
        {
            float currentTime = 0;

            while (currentTime < time && (condition == null || condition()))
            {
                foreach (ITimeAware timeAware in timeAwares)
                {
                    timeAware.Update(timeStep);
                }

                if (action != null)
                {
                    action();
                }

                currentTime += timeStep;
            }
        }
    }
}
